# 导入
import time
from wxauto import WeChat
import requests
# 获取微信窗口对象
wx = WeChat()
# 输出 > 初始化成功，获取到已登录窗口：xxxx
listen_list = [
    '有背背背'
]
for i in listen_list:
    wx.AddListenChat(who=i, savepic=True)

# 持续监听消息，并且收到消息后回复“收到”
wait = 1  # 设置1秒查看一次是否有新消息
while True:
    msgs = wx.GetListenMessage()
    for chat in msgs:
        who = chat.who  # 获取聊天窗口名（人或群名）
        one_msgs = msgs.get(chat)  # 获取消息内容
        # 回复收到
        for msg in one_msgs:
            msgtype = msg.type  # 获取消息类型
            print(msgtype)
            content = msg.content  # 获取消息内容，字符串类型的消息内容
            print(content)
            print(f'【{who}】：{content}')

            if msgtype == 'friend':
                url = f'http://api.qingyunke.com/api.php?key=free&appid=0&msg={content}'
                re = requests.get(url,params={'msg': content}).json()['content']
                chat.SendMsg(re)  # 回复收到
    time.sleep(1)
